<?php

class productsActions extends sfActions {
    
    public function executeIndex(sfWebRequest $request){
        $products = array();
        $c = new Criteria();
        
        $c->clear();
        $c->addAscendingOrderByColumn(ProductPeer::NAME);
        $products = ProductPeer::doSelect($c);
        $this->products = $products;
    }
    
    public function executeNew_step_1(sfWebRequest $request){
        $categories = array();
        $c = new Criteria();
        
        $c->clear();
        $c->addAscendingOrderByColumn(CategoryPeer::SEQUENCE);
        $categories = CategoryPeer::doSelect($c);
        $this->categories = $categories;
    }
    
    public function executeNew_step_1_save(sfWebRequest $request){
        $txtName = $request->getParameter('txtName');
        $txtUrl = $request->getParameter('txtUrl');
        $txtDescription = $request->getParameter('txtDescription');
        $rbCategory = $request->getParameter('rbCategory');
        $txtPrice = $request->getParameter('txtPrice');
        $c = new Criteria();
        
        //Tipo de publicación: Borrador
        $c->clear();
        $c->add(TypePeer::GROUP, 2);
        $c->add(TypePeer::VALUE, 1);
        $published_state = TypePeer::doSelectOne($c);
        
        $product = new Product();
        $product->setName($txtName);
        $product->setUrl($txtUrl);
        $product->setDescription($txtDescription);
        $product->setCategoryId($rbCategory);
        $product->setPrice($txtPrice);
        $product->setPublishedStateId($published_state->getId());
        $product->setEnabled(1);
        $product->save();
        
        $this->redirect("@products_new_step_2?product_id=".$product->getId());
    }

    public function executeNew_step_2(sfWebRequest $request){
        $product_id = $request->getParameter('product_id');
        $custom_attributes = array();
        $c = new Criteria();
        
        $c->clear();
        $c->addAscendingOrderByColumn(CategoryPeer::SEQUENCE);
        $categories = CategoryPeer::doSelect($c);
        $this->categories = $categories;
        
        $c->clear();
        $custom_attributes = CustomAttributePeer::doSelect($c);
        $this->custom_attributes = $custom_attributes;
        
        $c->clear();
        $c->add(ProductPeer::ID, $product_id);
        $c->add(ProductPeer::ENABLED, 1);
        $product = ProductPeer::doSelectOne($c);
        $this->product = $product;
    }
    
    public function executeLoad_custom_attribute_option(sfWebRequest $request){
        $custom_attribute_options = array();
        $custom_attribute_id = $request->getParameter('custom_attribute_id');
        $c = new Criteria();
        
        $c->clear();
        $c->add(CustomAttributeOptionPeer::CUSTOM_ATTRIBUTE_ID, $custom_attribute_id);
        $custom_attribute_options = CustomAttributeOptionPeer::doSelect($c);
        $this->custom_attribute_options = $custom_attribute_options;
    }
    
    public function executeShow(sfWebRequest $request){
        $product_attributes = array();
        $product_id = $request->getParameter('product_id');
        $c = new Criteria();
        
        $c->clear();
        $c->add(ProductPeer::ID, $product_id);
        $c->add(ProductPeer::ENABLED, 1);
        $product = ProductPeer::doSelectOne($c);
        $this->product = $product;
        
        $c->clear();
        $c->addAscendingOrderByColumn(CategoryPeer::SEQUENCE);
        $categories = CategoryPeer::doSelect($c);
        $this->categories = $categories;
        
        $c->clear();
        $c->add(ProductAttributePeer::PRODUCT_ID, $product_id);
        $product_attributes = ProductAttributePeer::doSelect($c);
        $this->product_attributes = $product_attributes;
    }
    
}